﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using DevExpress.Xpo;
using Zero.Core.Domain.Query;
using Zero.DataLayer.Donnees;
using Zero.POCO.Compagne;
using Zero.POCO.Interfaces;

namespace Zero.DataLayer.Repository
{
    public class PlanRdRepository : IRepositoryPlan<PlanRadio, Guid>
    {
        public PlanRadio FindBy(Guid id)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<PlanRadio> FindAll()
        {

            var config = new AppSettingsReader();
            var listEntities = new List<PlanRadio>();
            using (
                var uow = new UnitOfWork()
                    {
                        ConnectionString =
                            ((string)config.GetValue("connect", typeof(string)))
                    })
            {
                var xpoEntities = new XPCollection<XpoPlanRadio>(uow);
                listEntities.AddRange(MAP.MapInverse.MapPlanRd(xpoEntities));
            }return listEntities;
        }

        public IEnumerable<PlanRadio> FindBy(Query query)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<PlanRadio> FindBy(Query query, int index, int count)
        {
            throw new NotImplementedException();
        }

        public void SaveAll(IEnumerable<PlanRadio> entities)
        {
            throw new NotImplementedException();
        }

        public List<PlanRadio> FindAll(List<Query> entities)
        {
            throw new NotImplementedException();
        }

        public void Save(PlanRadio entity)
        {

            var config = new AppSettingsReader();
            using (
                var uow = new UnitOfWork()
                    {
                        ConnectionString =
                            ((string)config.GetValue("connect", typeof(string)))
                    })
            {
                var comp = MAP.Map.MapplanRadio(entity, uow);
                uow.CommitChanges();
            }
        }

        public void Add(PlanRadio entity)
        {
            throw new NotImplementedException();
        }

        public void Remove(PlanRadio entity)
        {
            throw new NotImplementedException();
        }

        public double GetTarif(Query query)
        {
            double prix = 0;
            var criterias = query.Criteria;
            var criteriaPlanTv = criterias.First(e => e.PropertyName == "PlanRd");
            var plantv = (PlanRadio)criteriaPlanTv.Value;
            var listeMap = plantv.ListeMapSupportTarifs;
            var criteriaInsertionTv = criterias.First(e => e.PropertyName == "InsertionRd");
            var insertiontvs = (List<InsertionRadio>)criteriaInsertionTv.Value;

            if (listeMap == null) return 0;
            foreach (var insertionTv in insertiontvs)
            {
                MapSupportTarif maptarif = null;
                IEnumerable<MapSupportTarif> maptarifs = listeMap.Where(e => e.SupportId == insertionTv.SupportRadio.id);
                if (maptarifs.Any())
                    maptarif = maptarifs.FirstOrDefault();
                else continue;
                if (maptarif != null)
                {
                    List<LignesTarifTv> tarifs = null;

                    tarifs = maptarif.TarifTv.LignesTarifTvs.Where(lt => lt.duree == insertionTv.Duree).ToList();
                    if (!tarifs.Any())
                        tarifs = maptarif.TarifTv.LignesTarifTvs.Where(lt => lt.duree == 0).ToList();
                    if (tarifs.Any())
                    {
                        var totalminutes = insertionTv.NumeroCinQMinute * 5;
                        var hours = totalminutes / 60;
                        var minutes = totalminutes % 60;
                        var debutSpan = new TimeSpan(hours, minutes, 0);

                        var LaLigne = tarifs.First(tr => tr.Debut <= debutSpan && tr.Fin > debutSpan);
                        prix = prix + LaLigne.Prix;
                    }

                }
            }
            return prix;
        }
    }
}